package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.DepositOrderSalesDetail;
import com.turing.post.salesmanage.util.DepositOrderSalesDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 充值单销售员明细（DepositOrderSalesDetail）
 * 
 * @author zxp
 * @Jan 17, 2013
 */

@Stateless(mappedName = "salesmanage.impl.DepositOrderSalesDetailDaoImpl")
@Remote
@Local
public class DepositOrderSalesDetailDaoImpl implements
		DepositOrderSalesDetailDao {

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteDepositOrderSalesDetail(DepositOrderSalesDetail dosd)
			throws PCPSApplicationException {
		DepositOrderSalesDetail _dosd = getDepositOrderSalesDetailByPrimaryKey(dosd);
		if (_dosd != null) {
			GeneralDao.remove(_em, _dosd);
		}
	}

	@Override
	public DepositOrderSalesDetail getDepositOrderSalesDetailByPrimaryKey(
			DepositOrderSalesDetail dosd) {
		return _em.find(DepositOrderSalesDetail.class, dosd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositOrderSalesDetail> getDepositOrderSalesDetailList(
			BigInteger depositOrderId, Integer page) {
		String sql = "select * from DepositOrderSalesDetail t where t.DepositOrderId="
				+ depositOrderId;
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, DepositOrderSalesDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositOrderSalesDetail> getDepositOrderSalesDetailListByDepositOrderId(
			BigInteger depositOrderId) {
		String sql = "select * from DepositOrderSalesDetail t where t.DepositOrderId="
				+ depositOrderId;
		Query query = _em.createNativeQuery(sql, DepositOrderSalesDetail.class);
		return query.getResultList();
	}

	@Override
	public BigInteger getDepositOrderSalesDetailTotal(BigInteger depositOrderId) {
		String sql = "select nvl(count(*),0) count from DepositOrderSalesDetail t where t.DepositOrderId="
				+ depositOrderId;
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertDepositOrderSalesDetail(DepositOrderSalesDetail dosd)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, dosd);
	}

	@Override
	public void updateDepositOrderSalesDetail(DepositOrderSalesDetail dosd)
			throws PCPSApplicationException {
		GeneralDao.update(_em, dosd);
	}
}